<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>K 和数对的最大数目</title>
</head>
<body>
  <script>
    function fn(nums, k) {
      var res = 0
      nums = nums.filter(a => a < k)
      for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== null) {
          let other = k - nums[i]
          nums[i] = null
          let idx = nums.indexOf(other)
          if (idx > -1) {
            nums[idx] = null
            res++
          }
        }
      }
      return res
    }
    console.log(fn([1, 2, 3, 4], 5))
  </script>
</body>
</html>